package com.sls.web.manager.recharge.service.impl;

import java.lang.reflect.Type;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.recharge.pojo.CardInfo;
import com.sls.web.manager.recharge.querybean.CardInfoQuery;
import com.sls.web.manager.recharge.service.CardInfoService;
import com.sls.web.manager.socket.SuperService;

/**
 * @author sxf
 * 
 */
@Service("cardInfoService")
public class CardInfoServiceImpl extends SuperService implements
		CardInfoService {
	private static Logger log = Logger.getLogger(CardInfoServiceImpl.class);

	/**
	 * @param serviceCode
	 * @param sendObj
	 * @param userInfoVO
	 * @param classType
	 *            类型信息(如:String,为String.class)
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public <T> T callService(String serviceCode, Head head, Object sendObj,
			UserInfoVO userInfoVO, Object... classType) throws Exception {
		Gson gson = CommonUtil.gson();
		if (head == null) {
			head = new Head();
			head.setRequestId(UUID.randomUUID().toString());
			head.setServiceCode(serviceCode);
			head.setIp(userInfoVO.getCurrentIp());
			head.setUserId(userInfoVO.getUserId());
		}
		SocketDTO requestDTO = super.send(head, sendObj);
		if (!TransmissionCode.SUCCESS.getCode().equals(
				requestDTO.getHead().getResponseCode())) {
			throw new ManagerException(requestDTO.getHead()
					.getResponseMessage());
		}
		String body = requestDTO.getBody();
		T t = null;
		if (body != null) {
			if (classType != null && classType.length > 0) {
				if (classType[0] instanceof Class) {
					t = gson.fromJson(body, (Class<T>) classType[0]);
				} else if (classType[0] instanceof Type) {
					t = gson.fromJson(body, (Type) classType[0]);
				}
			}
		}
		return t;
	}

	@Override
	public boolean saveCardInfo(CardInfo cardInfo, UserInfoVO userInfoVO)
			throws Exception {
		callService(Constants.INSERT_CARDINFO, null, cardInfo, userInfoVO);
		return true;
	}

	@Override
	public boolean insertCardInfoBatch(List<CardInfo> list,
			UserInfoVO userInfoVO) throws Exception {
		callService(Constants.INSERT_CARDINFO_BATCH, null, list, userInfoVO);
		return true;
	}

	@Override
	public List<CardInfo> queryCardInfo(CardInfoQuery query,
			UserInfoVO userInfoVO) throws Exception {
		log.info("Enter PermissionService queryCardInfo method...");
		List<CardInfo> list = callService(Constants.QUERY_CARDINFO_LIST, null,
				query, userInfoVO, new TypeToken<List<CardInfo>>() {
				}.getType());
		log.info("Leave PermissionService queryCardInfo method...");
		return list;
	}

	public String createCardInfoFileTxt(CardInfoQuery query,
			UserInfoVO userInfoVO) throws Exception {
		log.info("Enter PermissionService createCardInfoFileTxt method...");
		String filename = callService(Constants.CREATE_CARDINFO_FILE_TXT, null,
				query, userInfoVO, String.class);
		return filename;
	}

	@Override
	public Pagination<CardInfo> queryCardInfoPage(CardInfoQuery query,
			UserInfoVO userInfoVO) throws Exception {
		log.info("Enter PermissionService queryCardInfoPage method...");
		Pagination<CardInfo> CardInfoPage = callService(
				Constants.QUERY_CARDINFO_PAGE, null, query, userInfoVO,
				new TypeToken<Pagination<CardInfo>>() {
				}.getType());
		log.info("Leave PermissionService queryCardInfoPage method...");
		return CardInfoPage;
	}

	public boolean updateCardInfo(CardInfo cardInfo, UserInfoVO userInfoVO)
			throws Exception {
		if (cardInfo == null || StringUtils.isBlank(cardInfo.getCardId())) {
			log.error("传入对象为空或id为空");
			return false;
		}
		callService(Constants.UPDATE_CARDINFO, null, cardInfo, userInfoVO);
		return true;

	}

	public boolean updateCardInfoByImportBatchNum(CardInfo cardInfo,
			UserInfoVO userInfoVO) throws Exception {
		if (cardInfo == null
				|| StringUtils.isBlank(cardInfo.getCardImportBatchNum())) {
			log.error("传入对象为空或importBatchNum为空");
			return false;
		}
		callService(Constants.UPDATE_CARDINFO_ByImportBatchNum, null, cardInfo,
				userInfoVO);
		return true;

	}

	@Override
	public boolean deleteCardInfo(UserInfoVO userInfoVO) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}
}
